package com.ruoyi.special.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.domain.AddressInfo;
import com.ruoyi.db.domain.SpecialCostomInfo;
import com.ruoyi.db.domain.SpecialCostomItemInfo;
import com.ruoyi.db.mapper.*;
import com.ruoyi.special.service.ISpecialCostomItemInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 机构推送子任务Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-11
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class SpecialCostomItemInfoServiceImpl implements ISpecialCostomItemInfoService {
    @Resource
    private SpecialCostomItemInfoMapper specialCostomItemInfoMapper;
    @Resource
    private SpecialCostomInfoMapper specialCostomInfoMapper;
    @Resource
    private AddressInfoMapper addressInfoMapper;
    @Resource
    SpecialDataInfoMapper specialDataInfoMapper;
    @Resource
    SpecialDataApiInfoMapper specialDataApiInfoMapper;

    /**
     * 查询机构推送子任务
     *
     * @param id 机构推送子任务主键
     * @return 机构推送子任务
     */
    @Override
    public SpecialCostomItemInfo selectSpecialCostomItemInfoById(Long id) {
        SpecialCostomItemInfo specialCostomItemInfo = specialCostomItemInfoMapper.selectSpecialCostomItemInfoById(id);


        //把数据转为逗号
        specialCostomItemInfo.setInChannelArray(processArray(specialCostomItemInfo.getInChannelArray()));
        specialCostomItemInfo.setOutChannelArray(processArray(specialCostomItemInfo.getOutChannelArray()));
        specialCostomItemInfo.setInFlowRateType(processArray(specialCostomItemInfo.getInFlowRateType()));
        specialCostomItemInfo.setOutFlowRateType(processArray(specialCostomItemInfo.getOutFlowRateType()));

        return specialCostomItemInfo;
    }

    private String processArray(String jsonArray) {

        return StrUtil.isEmpty(jsonArray) ? "" :
                JSONUtil.toList(JSONUtil.parseArray(jsonArray), String.class).stream()
                        .collect(Collectors.joining(","));
    }

    /**
     * 查询机构推送子任务列表
     *
     * @param specialCostomItemInfo 机构推送子任务
     * @return 机构推送子任务
     */
    @Override
    public List<SpecialCostomItemInfo> selectSpecialCostomItemInfoList(SpecialCostomItemInfo specialCostomItemInfo) {
        return specialCostomItemInfoMapper.selectSpecialCostomItemInfoList(specialCostomItemInfo);
    }

    /**
     * 查询机构推送子任务列表
     *
     * @param specialCostomItemInfo 机构推送子任务
     * @return 机构推送子任务
     */
    @Override
    public List<Map<String, Object>> selectSpecialCostomItemInfoListMap(SpecialCostomItemInfo specialCostomItemInfo) {
        return specialCostomItemInfoMapper.selectSpecialCostomItemInfoListMap(specialCostomItemInfo);
    }

    /**
     * 任务查询信息
     *
     * @param specialCostomItemInfo
     * @return
     */
    @Override
    public List<Map<String, Object>> selectSpecialCostomItemInfoQueryListMap(SpecialCostomItemInfo specialCostomItemInfo) {

        // TODO: 2023/4/21 wrapper

        List<Map<String, Object>> mapList = specialCostomItemInfoMapper.selectSpecialCostomItemInfoQueryListMap(specialCostomItemInfo);

        mapList.stream().forEach(map -> {

            // 处理任务推送条件信息
            map.put("pushCondiInfo", "");

            String cityName = (String) map.get("cityName");
            StringBuilder pushCityInfo = new StringBuilder();
            if (StrUtil.isNotEmpty(cityName)) {
                List<String> cityCodeList = JSON.parseArray(cityName, String.class);
                if (CollUtil.isNotEmpty(cityCodeList)) {
                    for (String cityCode : cityCodeList) {
                        AddressInfo addressInfo = addressInfoMapper.selectAddressInfoByCodeValue(cityCode);
                        if (ObjectUtil.isNotEmpty(addressInfo)) {
                            pushCityInfo.append(addressInfo.getName()).append(" | ");
                        }
                    }
                }
            }
            map.put("pushCityInfo", pushCityInfo.toString());

            String outCityArray = (String) map.get("outCityArray");
            StringBuilder notPushCityInfo = new StringBuilder();
            if (StrUtil.isNotEmpty(outCityArray)) {
                List<String> cityCodeList = JSON.parseArray(outCityArray, String.class);
                if (CollUtil.isNotEmpty(cityCodeList)) {
                    for (String cityCode : cityCodeList) {
                        AddressInfo addressInfo = addressInfoMapper.selectAddressInfoByCodeValue(cityCode);
                        if (ObjectUtil.isNotEmpty(addressInfo)) {
                            notPushCityInfo.append(addressInfo.getName()).append(" | ");
                        }
                    }
                }
            }
            map.put("notPushCityInfo", notPushCityInfo.toString());
        });

        return mapList;
    }

    /**
     * 新增机构推送子任务
     *
     * @param specialCostomItemInfo 机构推送子任务
     * @return 结果
     */
    @Override
    public int insertSpecialCostomItemInfo(SpecialCostomItemInfo specialCostomItemInfo) {
        SpecialCostomInfo costomInfo = specialCostomInfoMapper.selectSpecialCostomInfoById(String.valueOf(specialCostomItemInfo.getInfoId()));
        specialCostomItemInfo.setDeptId(costomInfo.getDeptId());
        specialCostomItemInfo.setInfoType(costomInfo.getInfoType());

        String CityName = specialCostomItemInfo.getCityName();
        List <String> descinfo = addressInfoMapper.selectAddressNamesByCodeValues(CityName);
        specialCostomItemInfo.setDescInfo(descinfo.toString());

        /**
         * 数据处理
         */
        if (StrUtil.isNotEmpty(specialCostomItemInfo.getNotPushDayWeek())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getNotPushDayWeek(), ",");
            specialCostomItemInfo.setNotPushDayWeek(JSON.toJSONString(split.stream().collect(Collectors.toSet())));
        }
        if (StrUtil.isNotEmpty(specialCostomItemInfo.getLoanTime())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getLoanTime(), ",");
            specialCostomItemInfo.setLoanTime(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getSalary())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getSalary(), ",");
            specialCostomItemInfo.setSalary(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getJob())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getJob(), ",");
            specialCostomItemInfo.setJob(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getPurpose())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getPurpose(), ",");
            specialCostomItemInfo.setPurpose(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getEnterpriseOwner())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getEnterpriseOwner(), ",");
            specialCostomItemInfo.setEnterpriseOwner(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getZhimaScore())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getZhimaScore(), ",");
            specialCostomItemInfo.setZhimaScore(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getHuabei())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getHuabei(), ",");
            specialCostomItemInfo.setHuabei(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getBaitao())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getBaitao(), ",");
            specialCostomItemInfo.setBaitao(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getYuqi())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getYuqi(), ",");
            specialCostomItemInfo.setYuqi(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getHouse())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getHouse(), ",");
            specialCostomItemInfo.setHouse(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getCar())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getCar(), ",");
            specialCostomItemInfo.setCar(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getGjj())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getGjj(), ",");
            specialCostomItemInfo.setGjj(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getCityName())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getCityName(), ",");
            specialCostomItemInfo.setCityName(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getOutCityArray())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getOutCityArray(), ",");
            specialCostomItemInfo.setOutCityArray(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getOrFilter())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getOrFilter(), ",");
            specialCostomItemInfo.setOrFilter(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getLoanAmount())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getLoanAmount(), ",");
            specialCostomItemInfo.setLoanAmount(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getXueli())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getXueli(), ",");
            specialCostomItemInfo.setXueli(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getBaodan())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getBaodan(), ",");
            specialCostomItemInfo.setBaodan(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getShebao())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getShebao(), ",");
            specialCostomItemInfo.setShebao(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getInChannelArray())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getInChannelArray(), ",");
            specialCostomItemInfo.setInChannelArray(JSON.toJSONString(split.stream().collect(Collectors.toSet())));
        }
        if (StrUtil.isNotEmpty(specialCostomItemInfo.getOutChannelArray())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getOutChannelArray(), ",");
            specialCostomItemInfo.setOutChannelArray(JSON.toJSONString(split.stream().collect(Collectors.toSet())));
        }
        if (StrUtil.isNotEmpty(specialCostomItemInfo.getInFlowRateType())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getInFlowRateType(), ",");
            specialCostomItemInfo.setInFlowRateType(JSON.toJSONString(split));
        }
        if (StrUtil.isNotEmpty(specialCostomItemInfo.getOutFlowRateType())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getOutFlowRateType(), ",");
            specialCostomItemInfo.setOutFlowRateType(JSON.toJSONString(split));
        }


        specialCostomItemInfo.setCreateTime(DateUtils.getNowDate());
        return specialCostomItemInfoMapper.insertSpecialCostomItemInfo(specialCostomItemInfo);
    }

    /**
     * 修改机构推送子任务
     *
     * @param specialCostomItemInfo 机构推送子任务
     * @return 结果
     */
    @Override
    public int updateSpecialCostomItemInfo(SpecialCostomItemInfo specialCostomItemInfo) {


        if(StrUtil.isNotEmpty(specialCostomItemInfo.getCityName())){
            List<String> descinfo = addressInfoMapper.selectAddressNamesByCodeValues(specialCostomItemInfo.getCityName());
            specialCostomItemInfo.setDescInfo(descinfo.toString());
        }
        /**
         * 数据处理
         */
        if (StrUtil.isNotEmpty(specialCostomItemInfo.getNotPushDayWeek())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getNotPushDayWeek(), ",");
            specialCostomItemInfo.setNotPushDayWeek(JSON.toJSONString(split.stream().collect(Collectors.toSet())));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getLoanTime())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getLoanTime(), ",");
            specialCostomItemInfo.setLoanTime(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getSalary())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getSalary(), ",");
            specialCostomItemInfo.setSalary(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getJob())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getJob(), ",");
            specialCostomItemInfo.setJob(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getPurpose())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getPurpose(), ",");
            specialCostomItemInfo.setPurpose(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getEnterpriseOwner())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getEnterpriseOwner(), ",");
            specialCostomItemInfo.setEnterpriseOwner(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getZhimaScore())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getZhimaScore(), ",");
            specialCostomItemInfo.setZhimaScore(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getHuabei())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getHuabei(), ",");
            specialCostomItemInfo.setHuabei(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getBaitao())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getBaitao(), ",");
            specialCostomItemInfo.setBaitao(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getYuqi())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getYuqi(), ",");
            specialCostomItemInfo.setYuqi(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getHouse())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getHouse(), ",");
            specialCostomItemInfo.setHouse(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getCar())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getCar(), ",");
            specialCostomItemInfo.setCar(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getGjj())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getGjj(), ",");
            specialCostomItemInfo.setGjj(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getCityName())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getCityName(), ",");
            specialCostomItemInfo.setCityName(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getOutCityArray())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getOutCityArray(), ",");
            specialCostomItemInfo.setOutCityArray(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getOrFilter())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getOrFilter(), ",");
            specialCostomItemInfo.setOrFilter(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getLoanAmount())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getLoanAmount(), ",");
            specialCostomItemInfo.setLoanAmount(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getXueli())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getXueli(), ",");
            specialCostomItemInfo.setXueli(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getBaodan())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getBaodan(), ",");
            specialCostomItemInfo.setBaodan(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getShebao())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getShebao(), ",");
            specialCostomItemInfo.setShebao(JSON.toJSONString(split));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getInChannelArray())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getInChannelArray(), ",");
            specialCostomItemInfo.setInChannelArray(JSON.toJSONString(split.stream().collect(Collectors.toSet())));
        }
        if (StrUtil.isNotEmpty(specialCostomItemInfo.getOutChannelArray())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getOutChannelArray(), ",");
            specialCostomItemInfo.setOutChannelArray(JSON.toJSONString(split.stream().collect(Collectors.toSet())));
        }

        if (StrUtil.isNotEmpty(specialCostomItemInfo.getInFlowRateType())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getInFlowRateType(), ",");
            specialCostomItemInfo.setInFlowRateType(JSON.toJSONString(split));
        }
        if (StrUtil.isNotEmpty(specialCostomItemInfo.getOutFlowRateType())) {
            List<String> split = StrUtil.split(specialCostomItemInfo.getOutFlowRateType(), ",");
            specialCostomItemInfo.setOutFlowRateType(JSON.toJSONString(split));
        }

        specialCostomItemInfo.setUpdateTime(DateUtils.getNowDate());
        return specialCostomItemInfoMapper.updateSpecialCostomItemInfo(specialCostomItemInfo);
    }

    /**
     * 批量删除机构推送子任务
     *
     * @param ids 需要删除的机构推送子任务主键
     * @return 结果
     */
    @Override
    public int deleteSpecialCostomItemInfoByIds(String ids) {

        SpecialCostomItemInfo specialCostomItemInfo = new SpecialCostomItemInfo();
        specialCostomItemInfo.setStatus(0);
        specialCostomItemInfo.setId(Long.valueOf(ids));
        specialCostomItemInfo.setDelFlag(Long.valueOf(1));

        int i = specialCostomItemInfoMapper.updateSpecialCostomItemInfo(specialCostomItemInfo);


        return i;
    }

    /**
     * 删除机构推送子任务信息
     *
     * @param id 机构推送子任务主键
     * @return 结果
     */
    @Override
    public int deleteSpecialCostomItemInfoById(Long id) {
        return specialCostomItemInfoMapper.deleteSpecialCostomItemInfoById(id);
    }

    @Override
    public List<Map<String, Object>> selectCountListMap(String itemInfoId, String startDate, String endDate) {

        SpecialCostomItemInfo itemInfo = specialCostomItemInfoMapper.selectSpecialCostomItemInfoById(Long.valueOf(itemInfoId));

        List<Map<String, Object>> resultMap = new ArrayList<>();
        if (StrUtil.equals(itemInfo.getInfoType(), "API")) {
            Map<String, Object> totalMap = specialDataApiInfoMapper.selectSpecialDataApiInfoCountTotalMap(itemInfoId, startDate, endDate);
            resultMap = specialDataApiInfoMapper.selectSpecialDataApiInfoCountListMap(itemInfoId, startDate, endDate);
            resultMap.add(0, totalMap);
        } else {
            Map<String, Object> totalMap = specialDataInfoMapper.selectSpecialDataInfoCountTotalMap(itemInfoId, startDate, endDate);
            resultMap = specialDataInfoMapper.selectSpecialDataInfoCountListMap(itemInfoId, startDate, endDate);
            resultMap.add(0, totalMap);
        }

        return resultMap;
    }

    @Override
    public int updateByIds(String infoId, String starttime, String endtime) {
        return specialCostomItemInfoMapper.updateByIds(infoId,starttime,endtime);
    }


}
